package se.hiq.wineo.server;

import java.util.ArrayList;

import se.hiq.winedb.datamodel.Wine;
import se.hiq.winedb.datamodel.Wine.WineColor;
import se.hiq.wineo.shared.data.WineDescriptor;
import se.hiq.wineo.shared.data.WineDetails;

public class WineTools {

	public static WineDetails toWineDetails(Wine w) {
		return new WineDetails(w.getPrimaryKey(), 
							   w.getName(), 
							   toClientColor(w.getColor()), 
							   w.getYear(), 
							   w.getAlc(), 
							   w.getDescription());
	}
	
	public static WineDescriptor toWineDesciptor(Wine w) {
		return new WineDescriptor(w.getPrimaryKey(), w.getName(), toClientColor(w.getColor()), w.getYear());
	}
	
	public static WineDescriptor[] toWineDescriptors(Wine[] wineArr) {
		ArrayList<WineDescriptor> wines = new ArrayList<WineDescriptor>();
		for (Wine w : wineArr) {
			wines.add(WineTools.toWineDesciptor(w));
		}
		return wines.toArray(new WineDescriptor[wines.size()]);
	}
	
	public static se.hiq.wineo.shared.data.WineColor toClientColor(WineColor color) {
		if (color == null) { return null; }
		
		switch (color) {
		case RED: return se.hiq.wineo.shared.data.WineColor.RED;
		case WHITE: return se.hiq.wineo.shared.data.WineColor.WHITE;
		case ROSE: return se.hiq.wineo.shared.data.WineColor.ROSE;
		default: throw new RuntimeException("Unknown data in switch statement");
		}
	}
	
	public static WineColor toServerColor(se.hiq.wineo.shared.data.WineColor color) {
		if (color == null) { return null; }
		
		switch (color) {
		case RED: return WineColor.RED;
		case WHITE: return WineColor.WHITE;
		case ROSE: return WineColor.ROSE;
		default: throw new RuntimeException("Unknown data in switch statement");
		}
	}
	
	public static Wine toWine(WineDetails w) {
		return new Wine(w.getPrimaryKey(),
				   		w.getName(), 
				   		w.getYear(),
				   		w.getAlc(),
				   		toServerColor(w.getColor()), 
				   		w.getDescription());
	}
	
}
